<?php
namespace app\shop\model;
use app\BaseModel;
use think\facade\Db;



class AdminSellingWater extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_selling_water';

    /**
     * @param $value
     * @param $data
     * @return mixed
     * 状态
     */
    public function getWaitingAttr($value){
        if(in_array($value,[1,2])){
            $waiting = [1=>'未入账',2=>'已入账'];
            return $waiting[$value];
        }
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 状态
     */
    public function getTypeAttr($value){
        if(in_array($value,[1,2])){
            $type = [1=>'员工提成',2=>'一级提成'];
            return $type[$value];
        }
    }
    /**
     * User: chenniang
     * Date: 2019-09-09 17:17
     * @param $value
     * @return int
     * descption:int
     */
    public function getExtractAttr($value){
        return intval($value);
    }
    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取分销流水列表
     */
    public function waterList($dis,$page = 10) {
        $data = self::alias('a')
            ->leftJoin('longbing_card_user b' ,'a.user_id = b.id')
            ->leftJoin('longbing_card_user_info c' ,'a.user_id = c.fans_id')
            ->leftJoin('longbing_card_user d' ,'a.source_id = d.id')
            ->leftJoin('longbing_card_user_info f' ,'a.source_id = f.fans_id')
            ->leftJoin('longbing_card_shop_order e','e.id = a.order_id')
            ->where($dis)
            ->field(["a.*,b.nickName,c.name,ROUND(a.price * a.extract/100,2) as moeny,d.nickName as source_name,f.name as source_names,e.out_trade_no,e.transaction_id"])
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();
        if(!empty($data['data'])){
            foreach ($data['data'] as &$v){
                $v['name']        = !empty($v['name'])?$v['name']:$v['nickName'];
                $v['source_name'] = !empty($v['source_name_nick'])?$v['source_name_nick']:$v['source_name'];
                $v['waiting']     = $v['status'] == 1?$v['waiting']:'已退款';
                $v['moeny']       = $v['selling_type']==0?$v['moeny']:$v['extract_cash'];
            }
        }
        return $data;
    }



    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取分销流水列表
     */
    public function waterListV2($dis,$page = 10,$mapor=[]) {
        $data = self::alias('a')
            ->leftJoin('longbing_card_user b' ,'a.user_id = b.id')
            ->leftJoin('longbing_card_user_info c' ,'a.user_id = c.fans_id')
            ->leftJoin('longbing_card_user d' ,'a.source_id = d.id')
            ->leftJoin('longbing_card_user_info f' ,'a.source_id = f.fans_id')
            ->leftJoin('longbing_card_shop_order e','e.id = a.order_id')
            ->where($dis)
            ->where(function ($query) use ($mapor){
                $query->whereOr($mapor);
            })
            ->field(["a.*,b.nickName,c.name,ROUND(a.price * a.extract/100,2) as moeny,d.nickName as source_name,f.name as source_names,e.out_trade_no,e.transaction_id"])
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();
        if(!empty($data['data'])){
            foreach ($data['data'] as &$v){
               // $v['name']        = !empty($v['name'])?$v['name']:$v['nickName'];
                $v['source_name'] = !empty($v['source_name_nick'])?$v['source_name_nick']:$v['source_name'];
                $v['waiting']     = $v['status'] == 1?$v['waiting']:'已退款';
                $v['moeny']       = $v['selling_type']==0?$v['moeny']:$v['extract_cash'];
            }
        }
        return $data;
    }

    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取分销流水列表
     */
    public function storedWaterList($dis,$where,$page = 10) {
        $data = self::alias('a')
            ->leftJoin('longbing_card_user b' ,'a.user_id = b.id')
            ->leftJoin('longbing_card_user_info c' ,'a.user_id = c.fans_id')
            ->leftJoin('longbing_card_user d' ,'a.source_id = d.id')
            ->leftJoin('longbing_card_user_info f' ,'a.source_id = f.fans_id')
            ->leftJoin('longbing_card_member_stored_order e','e.id = a.order_id')
            ->leftJoin('longbing_card_user_phone g','g.user_id = a.user_id')
            ->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field(["a.*,b.nickName,g.phone,c.name,ROUND(a.price * a.extract/100,2) as moeny,d.nickName as source_name,f.name as source_names,e.order_code as out_trade_no,e.transaction_id"])
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();
        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['name']        = !empty($v['name'])?$v['name']:$v['nickName'];

                $v['source_name'] = !empty($v['source_name_nick'])?$v['source_name_nick']:$v['source_name'];

                $v['waiting']     = $v['status'] == 1?$v['waiting']:'已退款';

                $v['moeny']       = $v['selling_type']==0?$v['moeny']:$v['extract_cash'];
            }
        }
        return $data;
    }
    /**
     * @param $data
     * @return int|string
     * 编辑流水
     */
    public function waterUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;
    }

    /**
     * User: chenniang
     * Date: 2019-09-27 10:20
     * @param $dis
     * @return array
     * descption:查找流水记录
     */
    public function waterSelect($dis){
        $data = $this->where($dis)->select()->toArray();
        return $data;
    }

    /**
     * User: chenniang
     * Date: 2019-09-27 10:52
     * @param $dis
     * @return void
     * descption:退还流水
     */
    public function refundWater($dis){
        $water = $this->where($dis)->select()->toArray();
        if(!empty($water)){
            foreach ($water as $value){
                $this->waterUpdate(['id'=>$value['id']],['status'=>0]);
                $money  = $value[ 'price' ] * $value[ 'extract' ]/100;

                if($value['selling_type']==1){
                    $money = $value[ 'extract_cash' ];
                }
                $money  = sprintf( "%.2f", $money );

                $profit = Db::name('longbing_card_selling_profit')->where(['user_id' => $value['user_id']])->find();
                if (!empty($profit)&&$profit[ 'waiting' ]>0){
                    $waiting = $profit[ 'waiting' ] - $money;
                    $waiting = floatval($waiting)<0?0:floatval($waiting);
                    Db::name('longbing_card_selling_profit')->where(['id' => $profit['id']])->update(['waiting' => $waiting]);
                }
            }
        }
        return true;
    }



}